//
//  EvaluationViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/9.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "EvaluationViewController.h"
#import "EvaluationTableViewCell.h"
#import "EvaluationApi.h"

@interface EvaluationViewController ()

@end

@implementation EvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"全部评价 (0)";
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerCellWithNib:@"EvaluationTableViewCell"];
    
    [self addMj_footer];
    [self addMj_header];
    self.tableView.bounces = YES;
    [self loadData];
    
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    WeakSelf(self)
    if (!self.listApi) {
        self.listApi = [[EvaluationApi alloc]init];
    }
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            NSArray *arr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *dic in arr) {
                Evaluation *evaluation = [Evaluation new];
                [evaluation setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:evaluation];
            }
            self.title = [NSString stringWithFormat:@"全部评价 (%@)",[[request.responseJSONObject objectForKey:@"data"] objectForKey:@"total"]];
            [weakself.tableView reloadData];
            [weakself endRefreshing];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EvaluationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluationTableViewCell" forIndexPath:indexPath];
    Evaluation *evluation = self.dataArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:evluation.avatar]];
    cell.name.text = evluation.nickname;
    cell.time.text = evluation.created_at;
    cell.content.text = evluation.body;
    [cell starScore:evluation.score];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Evaluation *evluation = self.dataArray[indexPath.row];
    return 60 + [evluation.body heightForFontSize:15 andWidth:SCREEN_WIDTH - 32];
}

@end
